<!-- 使用 type="home" 属性设置首页，其他页面不需要设置，默认为page；推荐使用json5，更强大，且允许注释 -->
<route lang="json5" type="home">
	{
	style: {
	navigationStyle: 'custom',
	navigationBarTitleText: '首页',
	},
	}
</route>
<template>
	<view class="HomeClass">
		<view class="rechTop" :style="{ marginTop: safeAreaInsets?.top + 'px' }">
			<view class="imgTitele flex">
				<image src="/static/images/toptitleImage.png"></image>
				<view class="Rightimg">
					<image src="/static/images/login.png"></image>
					<image src="/static/images/Settings.png"></image>
				</view>
			</view>
		</view>
		<view class="HelloBox">
			<p>Hello,{{userNmae}}！</p>
			<p>欢迎来到日光洗衣所</p>
		</view>
		<view class="WalletBox">
			<view class="top-box">
				<view class="balance">
					<view class="baTop">
						<text>钱包余额</text>
						<img @click="toggleBalance"
							:src="showBalance ? '/static/images/open.png' : '/static/images/close.png'" />
					</view>
				</view>
				<view class="balan_bott">
					<view>
						<text v-if="Balance">$</text>
						<span class="spanClass" :class="{ 'hidden-balance': !showBalance }">
							{{ showBalance ? Balance : '****' }}
						</span>
					</view>
					<view>
						<button class="recharge" @click="JumpTo">
							<img src="/static/images/Stored.png" />充值
						</button>
					</view>
				</view>
			</view>
			<view class="bottom-box">
				<view class="transaction">
					<view class="Imgbox" @click="MyTrade">
						<img src="/static/images/Trade.png" />
						<text>交易记录</text>
						<img class="toright" src="/static/images/Toright.png" />
					</view>
					<view class="divider"></view>
					<view class="Imgbox" @click="Mydiscount">
						<img src="/static/images/myOffer.png" />
						<text>我的优惠</text>
						<img class="toright" src="/static/images/Toright.png" />
					</view>
				</view>
			</view>
		</view>
		<view class="Project-box">
			<view class="title">
				<view class="vertical"></view>
				<p>全部项目</p>
			</view>
			<view class="Function">
				<view class="Function_top">
					<view class="Fun_top_left" @click="handMessage">
						<view>
							<p>最新消息</p>
							<p>活动信息不漏接</p>
						</view>
						<view class="Funimg">
							<img src="/static/images/MessageLf.png" />
							<img src="/static/images/xiaoxi.png" />
						</view>
					</view>
					<view class="Fun_top_right">
						<view class="Rcenter Rtong">
							<view @click="DiscountCenter">
								<p>优惠中心</p>
								<p>洗衣天天享折扣</p>
							</view>
							<img src="/static/images/Discount.png" />
						</view>
						<view class="Rstatus Rtong" @click="Shopselection">
							<view >
								<p>机器状态</p>
								<p>查询状态最即时</p>
							</view>
							<img src="/static/images/Machine.png" />
						</view>
					</view>
				</view>
				<view class="Function_bott">
					<view class="Fun_bott_grid">
						<view class="Fun_bott_box" v-for="item in boxdata" :key="item" @click="Clickbox(item.navigUrl)">
							<view>
								<image :src="item.imaUrl" />
							</view>
							<p>{{item.title}}</p>
						</view>
					</view>

				</view>
			</view>
		</view>
	</view>
</template>

<script lang="ts" setup>
	import { TestEnum } from '@/typings'
	import PLATFORM from '@/utils/platform'
	import { ref, onMounted } from 'vue'
	import { httpGet, httpPost } from '@/utils/http'
	import { useUserStore } from '@/store'
	import { useToast } from 'wot-design-uni'
	defineOptions({
		name: 'Home',
	})
	onMounted(async() => {
		 const pages = getCurrentPages()
		  const currentPage = pages[pages.length - 1]
		  const options = currentPage.options
		  // 检查是否有LINE返回的code
		  if (options.code) {
			  console.log(userStore.userInfo.token);
		    if(!userStore.userInfo.token){
				handleFetchToken(options.code)
			}
		  }
		  if(userStore.userInfo.token){
			  await getbalance()
		  }
	})
	const toast = useToast()
	const handMessage=()=>{
		toast.warning('暂无内容')
	}
	const userStore = useUserStore()
	const userNmae = computed(() => userStore.userInfo.username)
	const handleFetchToken = async (code) => {
	  try {
	    uni.showLoading({
	      title: '加载中...',
	      mask: true,
		  duration: 2000
	    });
	    const response = await httpGet('/miniapp/token/', { code: code });
		  if (response.status_code === 200) {
		      // 存储token
		      userStore.setToken(response.data.access_token)
			  userStore.setUsername(response.data.user.username)
			 await  getbalance()
			  uni.showToast({
			  	title: response.msg,
			  	icon: 'none',
			  	duration: 2000
			  });
		    }
	  } catch (error) {
	    uni.showToast({ title: '登录失败', icon: 'none' })
	  } finally {
	    uni.hideLoading();
	  }
	};
	const Balance = ref('')
	const showBalance = ref(true)
	const toggleBalance = () => {
		showBalance.value = !showBalance.value
	}
	const getbalance =async () => {
		// debugger
		try {
			uni.showLoading({
				title: '加载中...',
				mask: true
			});
			const response = await httpPost(
				'/miniapp/get_balance/',
				{},
				{},
				{
					'Authorization': `Bearer ${userStore.userInfo.token}`,
					'Content-Type': 'application/json;charset=utf-8'
				}
			);
			console.log(response.data);
			Balance.value = response.data.recharge_balance 
		} catch (error) {
			uni.showToast({
				title: '请求失败，请稍后重试',
				icon: 'none',
				duration: 2000
			});
		} finally {
			uni.hideLoading();
		}
	}
	// 获取屏幕边界到安全区域距离
	const { safeAreaInsets } = uni.getSystemInfoSync()
	const getImageUrl = (name) => {
	  return new URL(`/src/static/images/${name}.png`, import.meta.url).href
	}
	const boxdata = ref([
		{
			title: '店内付款',
			imaUrl:getImageUrl('store'),
			navigUrl: "/pages/Payment/index"
		},
		{
			title: '线上充值',
			imaUrl: getImageUrl('Online'),
			navigUrl: "/pages/Recharge/Recharge"
		},
		{
			title: '日光门市',
			imaUrl:getImageUrl('Storeimg'),
			navigUrl:""
		},
		{
			title: '代洗烘折',
			imaUrl: getImageUrl('Cleaning'),
			navigUrl:""
		}
	])
	const JumpTo = () => {
		uni.navigateTo({
			url: '/pages/Recharge/Recharge',
		})
	}
	const MyTrade = () => {
		uni.navigateTo({
			url: '/pages/Order/index',
		})
	}
	const Mydiscount = () => {
		uni.navigateTo({
			url: '/pages/discount/index',
		})
	}
	const DiscountCenter=()=>{
		uni.navigateTo({
			url: '/pages/DiscountCenter/index',
		})
	}
	const Clickbox = (imte) => {
		console.log(imte);
		if(imte==''){
			toast.warning('暂无内容')
		}else{
			uni.navigateTo({
				url: imte,
			})
		}
	}
	const Shopselection=()=>{
		uni.navigateTo({
			url: '/pages/Shopselec/index',
		})
	}
</script>
<style scoped>
	:deep(uni-page-wrapper){
		background-image: url('/static/images/homeBg.png');
		background-size: 100% 100%;
	}
</style>
<style scoped lang="scss">
	
	.main-title-color {
		color: #d14328;
	}

	.HomeClass {
		padding: 0 40rpx;
		position: relative;
		min-height: 100vh;
		  &::before {
		    content: '';
		    position: fixed;
		    top: 0;
		    left: 0;
		    width: 100vw;
		    height: 100vh;
		    background: url('/static/images/homeBg.png') no-repeat center center/cover;
		    z-index: -1;
		  }
		.rechTop {
			height: 160rpx;
			position: relative;
			display: flex;
			flex-direction: column;
			justify-content: flex-end;
			align-items: center;

			.imgTitele {
				width: 100%;
				margin-bottom: 35rpx;
				display: flex;
				justify-content: space-between;
				align-items: center;

				>image {
					width: 360rpx;
					height: 40rpx;
					position: absolute;
					left: 50%;
					transform: translateX(-50%);
				}

				.Rightimg {
					display: flex;
					gap: 20rpx;
					margin-left: auto;

					image {
						width: 40rpx;
						height: 40rpx;
					}
				}
			}
		}

		.HelloBox {
			margin-top: 30rpx;
			color: rgba(255, 255, 255, 1);
			font-weight: 700;
			p:first-child {
				font-size: 60rpx;
			}

			p {
				font-size: 42rpx;
			}
		}

		.WalletBox {
			position: relative;
			margin-top: 60rpx;
			color: white;
			min-height: 200rpx;
		}

		.top-box {
			background: linear-gradient(90deg, rgba(198, 147, 66, 1) 0%, rgba(237, 196, 138, 1) 100%);
			padding: 40rpx;
			border-radius: 20rpx;
			position: relative;
			z-index: 2;
			height: 180rpx;
			box-shadow: 0 4px 12px rgba(0, 0, 0, 0.4);

			.balan_bott {
				display: flex;
				align-items: flex-end;
				justify-content: space-between;
				height: calc(100% - 45rpx);

				.spanClass {
					font-size: 80rpx;
					margin-left: 10rpx;
					line-height: 1;
					vertical-align: bottom;
					display: inline-block;

					&.hidden-balance {
						line-height: 1;
						vertical-align: bottom;
						display: inline-block;
						letter-spacing: 2rpx; // 可选：增加星号间距
					}
				}
			}

			.balan_bott>text {
				font-size: 36rpx;
			}
		}

		.balance text:first-child {
			font-size: 14px;
		}

		.balance text:last-child {
			font-size: 30rpx;
			font-weight: bold;
		}

		.baTop {
			display: flex;
			align-items: center;
			cursor: pointer;

			img {
				margin-left: 10rpx;
				width: 24rpx;
				height: 16rpx;
				margin-top: 5rpx;
				transition: all 0.3s;
			}

			&:active {
				opacity: 0.7;
			}
		}

		.recharge {
			background-color: white;
			color: #e0a800;
			border: none;
			border-radius: 30px;
			cursor: pointer;
			font-size: 28rpx;
			display: flex;
			padding: 0 40rpx;
			align-items: center;
			gap: 10rpx;

			img {
				width: 32rpx;
				height: 32rpx;
			}
		}

		.bottom-box {
			background: linear-gradient(180deg, rgba(235, 172, 70, 1) 0%, rgba(237, 172, 66, 0.34) 100%);
			padding: 10px 20px;
			border-radius: 20rpx;
			margin-top: -30rpx;
			position: relative;
			height: 80rpx;
			z-index: 1;
		}

		.transaction {
			height: 100%;
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-top: 14rpx;

			img {
				width: 28rpx;
				height: 28rpx;
			}

			.Imgbox {
				display: flex;
				align-items: center;
				gap: 15rpx;
				cursor: pointer;

				.toright {
					width: 60rpx;
					height: 28rpx;
					margin-left: 20rpx;
				}
			}

			.divider {
				width: 48rpx;
				height: 0px;
				opacity: 0.2;
				transform: rotate(90deg);
				border: 1px solid rgba(255, 255, 255, 1);
			}
		}

		.transaction text {
			font-size: 28rpx;
		}

		.Project-box {
			.title {
				color: white;
				display: flex;
				align-items: center;
				gap: 10rpx;
				margin-top: 48rpx;

				.vertical {
					width: 8rpx;
					height: 32rpx;
					opacity: 1;
					background: rgba(199, 148, 67, 1);
				}
			}

			.Function {
				width: 100%;
				height: 200rpx;
				margin-top: 32rpx;

				.Function_top {
					display: flex;
					height: 320rpx;
					gap: 20rpx;

					.Fun_top_left {
						width: 50%;
						padding: 48rpx;
						border-radius: 20rpx;
						box-sizing: border-box;
						background: linear-gradient(135deg, rgba(24, 24, 24, 0.6) 0%, rgba(99, 99, 99, 0.6) 100%);
						border: 1px solid rgba(255, 255, 255, 0.6);

						p:first-child {
							font-size: 36rpx;
							font-weight: 700;
							color: rgba(255, 255, 255, 1);
						}

						p {
							font-size: 24rpx;
							color: rgba(255, 255, 255, 0.6);
						}

						.Funimg {
							display: flex;
							align-items: flex-end;
							justify-content: space-between;
							margin-top: 50rpx;

							img:first-child {
								width: 44rpx;
								height: 44rpx;
							}

							img {
								width: 80rpx;
								height: 80rpx;
							}
						}
					}
				}

				.Fun_top_right {
					width: 50%;
					height: 100%;

					.Rcenter {
						width: 100%;
						height: calc(50% - 10rpx);
					}

					.Rstatus {
						width: 100%;
						height: calc(50% - 8rpx);
						margin-top: 13rpx;
					}

					.Rtong {
						cursor: pointer;
						padding: 25rpx;
						box-sizing: border-box;
						border-radius: 20rpx;
						background: linear-gradient(135deg, rgba(24, 24, 24, 0.6) 0%, rgba(99, 99, 99, 0.6) 100%);
						border: 2rpx solid rgba(255, 255, 255, 0.6);
						display: flex;
						align-items: center;
						justify-content: space-between;

						p:first-child {
							font-size: 32rpx;
							font-weight: 700;
							color: rgba(255, 255, 255, 1);
						}

						p {
							font-size: 24rpx;
							color: rgba(255, 255, 255, 0.6);
						}

						img:first-child {
							width: 80rpx;
							height: 80rpx;
						}

						img {
							width: 33px;
							height: 39px;
						}
					}
				}

				.Function_bott {
					height: 200rpx;
					margin-top: 20rpx;

					.Fun_bott_grid {
						display: flex;
						flex-wrap: wrap;
						justify-content: space-between;
						gap: 10rpx;

						.Fun_bott_box {
							width: calc(25% - 15rpx);
							height: 170rpx;
							border-radius: 20rpx;
							background: linear-gradient(135deg, rgba(24, 24, 24, 0.6) 0%, rgba(99, 99, 99, 0.6) 100%);
							border: 2rpx solid rgba(255, 255, 255, 0.6);
							color: white;
							display: flex;
							flex-direction: column;
							align-items: center;
							justify-content: center;

							// padding: 0 20rpx;
							image {
								width: 76rpx;
								height: 70rpx;
							}
							p{
								margin-top: 10rpx;
							}
						}
					}
				}
			}
		}
	}
</style>